/**
 * 
 */
package org.ejc.struts.admin;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.actions.DispatchAction;
import org.ejc.api.executive.ConventionExecutiveAPIUtil;
import org.ejc.api.registration.RegistrationAPIUtil;
import org.ejc.api.transaction.TransactionAPIUtil;
import org.ejc.persist.model.ConventionExecutive;
import org.ejc.session.EJCSession;
import org.ejc.struts.dashboard.DashboardAction;

/**
 * @author Brian Boyle
 * @struts.action path="/struts/admin/updateExecutiveAction" scope="request"
 *                parameter="methodToCall" name="createExecutiveForm"
 *                validate="false"
 *                input="/struts/admin/editExecutive.jsp"
 * 
 * @struts.action-forward name="preUpdateExecSuccess" path="/struts/admin/editExecutive.jsp"
 * @struts.action-forward name="updateExecSuccess" path="/struts/admin/editExecutive.jsp"
 * @struts.action-forward name="viewExecTransactionsSuccess" path="/struts/dashboard/drillDownTxs.jsp"
 */
public class UpdateExecutiveAction extends DispatchAction {

	public final ActionForward preUpdateExec(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception 
	{
		HttpSession session = request.getSession();
		EJCSession ejcSession = (EJCSession) session.getAttribute("ejcSession");
		ejcSession.setCurrentAction("Update Executive");
		//TODO This does not work for returning from the transaction screen
		ConventionExecutive exec = null;
		CreateExecutiveForm execForm = (CreateExecutiveForm)form;
		//if(ejcSession.getSelectedExecutive() != null){
		//	exec = ConventionExecutiveAPIUtil.getLocalHome().create().getExecutive(ejcSession.getSelectedExecutive().getExecutiveId());
		//}else{
			exec = ConventionExecutiveAPIUtil.getLocalHome().create().getExecutive(execForm.getExecutiveId());
		//}
		ejcSession.setSelectedExecutive(exec);
		setupExecutiveDetails(execForm, exec);
		return mapping.findForward("preUpdateExecSuccess");
	}

	private void setupExecutiveDetails(CreateExecutiveForm form, ConventionExecutive executive)
	{
		form.setAdministrator(executive.isAdmin());
		form.setFirstName(executive.getFirstName());
		form.setLastName(executive.getLastName());
		form.setLanguageId(executive.getLanguageIso().getLanguageId());
		form.setUserName(executive.getUsername());
		form.setPassword1(executive.getPassWord());
		form.setPassword2(executive.getPassWord());
	}
	
	public final ActionForward updateExec(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception 
	{
		CreateExecutiveForm execForm=(CreateExecutiveForm)form;
		ConventionExecutive exec=execForm.copyToExecutive();
		ConventionExecutiveAPIUtil.getLocalHome().create().updateConventionExecutive(exec);
		
		ActionMessages messages = new ActionMessages();
		ActionMessage msg = new ActionMessage("info.register.updateSuccessful");
		messages.add("message1", msg);
		saveMessages(request, messages);
		return mapping.findForward("updateExecSuccess");
	}
	
	public final ActionForward loadExecTransactions(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		//Load the executive transaction information
		HttpSession session = request.getSession();
		EJCSession ejcSession = (EJCSession) session.getAttribute("ejcSession");
		Integer executiveId = ejcSession.getSelectedExecutive().getExecutiveId();
		log.debug("Lookin for "+executiveId);
		List listing=TransactionAPIUtil.getLocalHome().create().getCurrentSessionTransactionsForExecutive(executiveId, 0, 0);
		request.setAttribute("txListing",listing);

		Date fromDate=RegistrationAPIUtil.getLocalHome().create().getLastActiveDate();
		Date toDate=RegistrationAPIUtil.getLocalHome().create().getLastInActiveDate();
		
		request.setAttribute("executive",ejcSession.getSelectedExecutive());
		request.setAttribute("toDate",toDate);
		request.setAttribute("fromDate",fromDate);
		return mapping.findForward("viewExecTransactionsSuccess");
	}
	

}
